package ext.tianma.part.ecn;

import ext.com.core.CoreUtil;
import ext.tianma.part.PartWorkflowUtil;
import ext.tianma.util.PropertiesUtil;
import java.io.File;
/**
 * 用来修改sourcing驳回废弃,修改中间表生命周期LIFECYCLE为REJECTED
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import wt.fc.Persistable;
import wt.fc.collections.WTArrayList;
import wt.log4j.LogR;
import wt.part.WTPart;
import wt.lifecycle.State;
import wt.lifecycle.LifeCycleHelper;

public class UpateRejectLifecycle {
	
	private static final Logger log = LogR.getLogger(UpateRejectLifecycle.class.getName());
	public static String configFilePath = "codebase" + File.separator + "ext" + File.separator + "tianma"
			+ File.separator + "cfg" + File.separator + "TianmaPro.properties";
	private static final String REJECTED = "REJECTED";// 已否决
	
	
	public static boolean updateLifecycle(WTArrayList parts) {
		System.out.println("===进入程序废弃设置生命周期阶段==="+parts.toString());
		System.out.println("===进入程序废弃设置生命周期阶段==="+parts);
		boolean flag = false;
		Connection conn = null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			 PropertiesUtil ut = new PropertiesUtil(configFilePath);
			 String url = ut.getValueByKey("TM_JDBC_URL");
			 String user = ut.getValueByKey("TM_JDBC_USER");
			 String pwd = ut.getValueByKey("TM_JDBC_PWD");
			//conn = DriverManager.getConnection("jdbc:oracle:thin:@10.100.6.184:1521:wind", "plm2erp", "plm2erp");
			 conn = DriverManager.getConnection(url, user, pwd);
			int size = parts.size();
			System.out.println("===part的个数==="+size);
			for (int i = 0; i < size; i++) {
				System.out.println("===进去循环,开始修改数据库===");
				Persistable persistable = parts.getPersistable(i);
				System.out.println("==persistable=="+persistable);
				if (persistable != null && persistable instanceof WTPart) {
					WTPart part = (WTPart) persistable;
					// 1.设置状态
					State state = State.toState(REJECTED);
					if (!state.equals(part.getLifeCycleState())) {
						part = (WTPart) LifeCycleHelper.service.setLifeCycleState(part, state);

			        //partleast = CoreUtil.getWTPartByNumberAndView(part.getNumber(), "Design");// 获取design试图
					//WTArrayList partList = PartWorkflowUtil.getAllPartViews(part);// 获取所有视图
					System.out.println("===WTPart part = (WTPart) persistable;==="+part);
					String SQL = "update pdm_erp_part set LIFECYCLE = 'REJECTED',FLAG_SOURCING = 'N',SOURCINGREJECT_FLAG = '40' where item_id = ? and pview = ? ";
					System.out.println("==sql=="+SQL);
					int rs = 0;
					PreparedStatement pstmt = null;
					pstmt = conn.prepareStatement(SQL);
					System.out.println("==pstmt=="+pstmt);
					System.out.println("==part.getNumber()=="+part.getNumber());
					System.out.println("==part.getViewName()=="+part.getViewName());
					pstmt.setString(1, part.getNumber());
					pstmt.setString(2, part.getViewName());
					System.out.println("开始执行sql"+pstmt);
					rs = pstmt.executeUpdate();
					System.out.println("===已修改数据库数据==rs="+rs);
					log.debug("==============设置废弃状态的部件数=============" + rs);
					if (rs != 0) {
						flag = true;
					}
					pstmt.close();
					pstmt = null;
				}
			}
		    }
			conn.close();
			conn = null;
		} catch (Exception e1) {
			// trans.rollback();
			flag = false;
			e1.printStackTrace();
			System.out.println("===========修改中间表生命周期失败=====" + e1);
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("==数据库链接未关闭==");
			}
		}

		return flag;
	}

}
